let config = require('./config.js');
let util = require('./util.js');
let API_URL = config.API_URL;
let userInfo;
function goToCer(that) {
  my.confirm({
    title: '提示',
    content: '您还没有实名认证',
    confirmButtonText: '去认证',
    cancelButtonText: '取消',
    success: function (res) {
      if (res.confirm) {
        my.navigateTo({
          url: '/pages/user/certification/certification',
        })
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
}
function goToTel() {
  my.confirm({
    title: '提示',
    content: '您还没有绑定手机号',
    confirmButtonText: '去认证',
    cancelButtonText: '取消',
    success: function (res) {
      if (res.confirm) {
        my.navigateTo({
          url: '/pages/user/account/bind/bind?name=手机&typ=mobile',
        })
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
}
function goToLive(mobile) {
  my.confirm({
    title: '提示',
    content: '您还没有绑定直播间',
    confirmButtonText: '去绑定',
    cancelButtonText: '取消',
    success: function (res) {
      if (res.confirm) {
        my.navigateTo({
          url: '/pages/user/bind-live/bind-live?phone=' + mobile,
        })
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
}

function goToLogin() {
  let page = getCurrentPages();
  let length = page.length;
  let app = getApp();
  if (length == 1) {
    app.globalData.routeF = 'switchTab';
  } else {
    app.globalData.routeF = 'redirectTo';
  }
  var url = '/' + page[length - 1].route;
  var options = page[length - 1].options;
  if (options != undefined) {
    url += '?';
    for (var key in options) {
      url = url + key + '=' + options[key] + '&';
    }
    url = url.slice(0, -1)
  }
  app.globalData.route = url;
  wx.redirectTo({
    url: '/pages/login/index',
  })
}

function addI(id) {
  // 把电话号码传递给跳转之后的页面
  userInfo = my.getStorageSync({ key: 'user' });
  console.log(userInfo)
  if (check()) {
    util.PRL(API_URL.add_invite, { demand_id: id }, true)
      .then(res => {
        console.log(res)
        if (res.data.status == 200) {
          my.showToast({
            content: '邀请成功',
            duration: 1200
          })
        } else if (res.data.status == 300) {
          if (res.data.msg == '绑定的直播间已失效请重新绑定') {
            my.confirm({
              title: '提示',
              content: res.data.msg,
              confirmButtonText: '重新绑定',
              cancelButtonText: '取消',
              success: function (res) {
                if (res.confirm) {
                  my.navigateTo({
                    url: '/pages/user/bind-live/bind-live?phone=' + userInfo.data.phone,
                  })
                }
              }
            })
          } else {
            my.confirm({
              title: '提示',
              content: res.data.msg,
              confirmButtonText: '去绑定',
              cancelButtonText: '取消',
              success: function (res) {
                if (res.confirm) {
                  my.navigateTo({
                    url: '/pages/user/bind-live/bind-live?phone=' + userInfo.data.phone,
                  })
                }
              }
            })
          }
        }
      }).catch(res => {
        console.log(res);
      })
  }
}
// 推荐
function addR(id) {
  if (!checkLogin()) {
    return
  }
  console.log(id)
  let hasStore = my.getStorageSync({ key: 'hasStore' });
  console.log(hasStore)
  let itemList;
  if (hasStore) {
    itemList = ["从手机相册选择", "从我的店铺选择"];
    my.showActionSheet({
      title: '文件上传',
      items: itemList,
      cancelButtonText: '取消好了',
      success: (res) => {
        console.log(res)
        if (res.index == 1) {
          my.navigateTo({
            url: '/pages/demand/recommend/good?demandId=' + id,
          })
        } else if (res.index == 0) {
          my.navigateTo({
            url: '/pages/demand/recommend_news/recommend_news?demandId=' + id,
          })
        }
      },
    });
  } else {
    itemList = ["从手机相册选择"];
    my.navigateTo({
      url: '/pages/demand/recommend_news/recommend_news?demandId=' + id,
    })
  }
}

function checkLogin() {
  userInfo = my.getStorageSync({ key: 'user' });
  if (userInfo == "") {
    goToLogin()
    return false;
  } else {
    return true;
  }
}
// 检查登录之后的 电话 身份证 直播间的绑定
function check() {
  let hasIdentified = my.getStorageSync({ key: 'hasIdentified' });
  let hasBindLive = my.getStorageSync({ key: 'hasBindLive' });
  if (!checkLogin()) {
    return false;
  } else if (userInfo.phone == '') {
    goToTel();
    return false;
  } else if (!hasIdentified) {
    goToCer();
    return false;
  } else if (!hasBindLive) {
    goToLive(userInfo.phone);
    return false;
  } else {
    return true;
  }
}

function bindLive() {
  if (check()) {
    my.navigateTo({
      url: '/pages/user/bind-live/bind-live?phone=' + userInfo.phone,
    })
  }
}



module.exports = {
  addI: addI,
  check: check,
  bindLive: bindLive,
  addR: addR
}